package com.example.supermarket.controller;

import com.example.supermarket.entity.Goods;
import com.example.supermarket.mapper.GoodsMapper;
import com.example.supermarket.service.AdminGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>后台商品管理模块</p>
 * @author Bosen
 * @date 2021/10/12 19:30
 */
@RestController
public class AdminGoodsController {

    @Autowired
    AdminGoodsService service;

    @Autowired
    GoodsMapper goodsMapper;

    /**
     * <p>商品管理页面</p>
     */
    @RequestMapping("/admin/goods")
    public ModelAndView goods(ModelAndView modelAndView) {
        // 获取所有商品信息
        List<Goods> goodsList = service.findAll();
        modelAndView.addObject("goodsList", goodsList);

        modelAndView.setViewName("admin/goods");
        return modelAndView;
    }

    /**
     * <p>增加商品</p>
     */
    @RequestMapping("/admin/add/goods")
    public ModelAndView addGoods(ModelAndView modelAndView, String name, String describe,
                                 String price, MultipartFile img){
        // 类型转换
        BigDecimal priceRes = null;
        try {
            priceRes = new BigDecimal(price);
        }catch (Exception e){
            modelAndView.addObject("msg","输入的金额不合法");
            modelAndView.setViewName("redirect:/admin/goods");
            return modelAndView;
        }
        // 添加商品
        int res = service.addGoods(name, describe, priceRes, img);
        switch (res){
            case -1:
                modelAndView.addObject("msg","图片上传失败");
                break;
            case 0:
                modelAndView.addObject("msg","网络超时");
                break;
            case 1:
                modelAndView.addObject("msg","添加商品成功");
                break;
            case 2:
                modelAndView.addObject("msg","必填信息不能为空");
                break;
            default:
                break;
        }
        modelAndView.setViewName("redirect:/admin/goods");
        return modelAndView;
    }

    /**
     * <p>通过id值删除商品</p>
     */
    @RequestMapping("/admin/del/goods")
    public ModelAndView delGoods(ModelAndView modelAndView,int id){
        int res = service.delGoods(id);
        if (res == 1){
            modelAndView.addObject("msg","删除成功");
        }else{
            modelAndView.addObject("msg","删除失败");
        }
        modelAndView.setViewName("redirect:/admin/goods");
        return modelAndView;
    }

    /**
     * <p>修改商品信息</p>
     */
    @RequestMapping("/admin/update/goods")
    public ModelAndView updateGoods(Integer id, ModelAndView modelAndView, String name, String describe,
                                    String price, MultipartFile img){
        // 类型转换
        BigDecimal priceRes = null;
        try {
            priceRes = new BigDecimal(price);
        }catch (Exception e){
            modelAndView.addObject("msg","输入的金额不合法");
            modelAndView.setViewName("redirect:/admin/goods");
            return modelAndView;
        }
        // 修改商品信息
        int res = service.updateGoods(id, name, describe,priceRes,img);
        switch (res){
            case -1:
                modelAndView.addObject("msg","图片上传失败");
                break;
            case 0:
                modelAndView.addObject("msg","网络超时");
                break;
            case 1:
                modelAndView.addObject("msg","修改商品成功");
                break;
            case 2:
                modelAndView.addObject("msg","必填信息不能为空");
                break;
            default:
                break;
        }
        modelAndView.setViewName("redirect:/admin/goods");
        return modelAndView;
    }
}
